﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using QuadRemote.Core;

namespace QuadRemote.Panels
{
    class EnginePanel : QuadPanel
    {
        public EnginePanel() : base("Engines",180,180, Color.Red,25)
        {
            //resize.Hide();
            _minWidth = _maxWidth = 180;
            _minHeight = _maxHeight = 180;
            //_resizable = false;
            //resize.Hide();

            int y = 6-16;
            int h = 16;


            Label n = new Label();
            n.ForeColor = Color.White;
            n.Top = 118;
            n.Left = 160;
            n.Font = new Font("Calibri", 8);
            n.Text = "LIPO";
            n.Width = 60;
            n.TextAlign = ContentAlignment.MiddleRight;
            drawingPanel.Controls.Add(n);

            mode = new Label();
            mode.Font = new Font("Calibri", 8, FontStyle.Bold);
            mode.Width = 20;
            mode.TextAlign = ContentAlignment.MiddleCenter;
            mode.Left = 6 + 54 + 9;
            mode.Top = 6 + 54 + 6;
            mode.ForeColor = Color.Black;
            mode.BackColor = Color.Transparent;
            drawingPanel.Controls.Add(mode);
        }

        private Label createLabel(int x, int y, string name)
        {
            Label n = new Label();
            n.ForeColor = Color.White;
            n.Top = y;
            n.Left = x;
            n.Font = new Font("Calibri", 8);
            n.Text = name;
            n.Width = 60;
            n.TextAlign = ContentAlignment.MiddleRight;
            drawingPanel.Controls.Add(n);

            n = new Label();
            n.ForeColor = Color.Lime;
            n.Top = y;
            n.Left = x + 60;
            n.Width = 60;
            n.Font = new Font("Calibri", 8);
            n.TextAlign = ContentAlignment.MiddleLeft;
            return n;
        }

        private float r1 = 0;
        private float r2 = 0;
        private float r3 = 0;
        private float r4 = 0;

        private Label mode;

        public void drawLipos(Graphics g, int offsetX, int offsetY, int width)
        {
            float c = 0; // Core.RemoteController.instance().latestState.vCell1;
            drawLipo(g, offsetX, offsetY, width, c);
            //c = Core.RemoteController.instance().latestState.vCell2;
            drawLipo(g, offsetX, offsetY+7, width, c);
            //c = Core.RemoteController.instance().latestState.vCell3;
            drawLipo(g, offsetX, offsetY+14, width, c);
            //c = Core.RemoteController.instance().latestState.vCell4;
            if (c > 0)
                drawLipo(g, offsetX, offsetY+21, width, c);
        }

        public void drawLipo(Graphics g, int offsetX, int offsetY, int width, float val)
        {
            Pen p = (val > 3.8) ? Pens.Lime : (val > 3.4) ? Pens.Orange : Pens.Red;
            Brush b = (val > 3.8) ? Brushes.Lime : (val > 3.4) ? Brushes.Orange : Brushes.Red;

            float minVolt = 3.3F;
            float maxVolt = 4.3F;
            if (val > maxVolt)
            {
                val = maxVolt;
                p = Pens.Yellow;
                b = Brushes.Yellow;
            }
            val -= minVolt;
            if (val < 0) val = 0;
            g.DrawRectangle(p, offsetX, offsetY, width, 4);
            g.FillRectangle(b, offsetX, offsetY, val * width / (maxVolt - minVolt), 4);
        }

        public void drawEngine(Graphics g, int offsetX, int offsetY, int radius, float e, bool on, float r)
        {
            Brush b;
            Pen p;

            if (!on)
            {
                b = new SolidBrush(Color.FromArgb(0, 0, 0));
                //p = new Pen(Color.FromArgb(128, 128, 128), 1);
            }
            else if (e == 0.0)
            {
                b = new SolidBrush(Color.FromArgb(0, 128, 255));
                //p = new Pen(Color.FromArgb(0, 64, 128), 2);
            }
            else
            {
                b = new SolidBrush(Color.FromArgb((int)(e * 255), (int)((1 - e) * 255), 0));
                //p = new Pen(Color.FromArgb((int)(e * 255), (int)((1 - e) * 255), 0), 2);
            }


            if (e > 0.0)
            {
                for (int i = 0; i < e * 10; i+=2)
                    g.DrawEllipse(new Pen(Color.FromArgb((int)((e*10-i)*25),(int)(e*255),(int)((1-e)*255),0),2), offsetX - radius / 2 - i, offsetY - radius / 2 - i, radius + i * 2, radius + i * 2);
            }

            g.FillEllipse(b, offsetX - radius / 2, offsetY - radius / 2, radius, radius);

            int radius2 = radius - 6;
            float speed = 3F;
            for (int i=0; i < 10;i++)
                g.DrawLine(new Pen(Color.FromArgb((int)(25.5 * (10 - i)), Color.White)), offsetX + (float)(Math.Sin(r / speed - e * i / 10.0) * radius2 / 2), offsetY - (float)Math.Cos(r / speed - e * i / 10.0) * radius2 / 2,
               offsetX - (float)Math.Sin(r / speed - e * i / 10.0) * radius2 / 2, offsetY + (float)Math.Cos(r / speed - e * i / 10.0) * radius2 / 2);
            
            /*g.DrawLine(Pens.White, offsetX + (float)(Math.Sin(r / speed) * radius2 / 2), offsetY - (float)Math.Cos(r / speed) * radius2 / 2,
                           offsetX - (float)Math.Sin(r / speed) * radius2 / 2, offsetY + (float)Math.Cos(r / speed) * radius2 / 2);
            */
        }

        public void drawEngines(Graphics g, int offsetX, int offsetY, int radius, int spacing)
        {
            int x1 = offsetX + radius / 2;
            int x2 = offsetX + spacing + radius + radius / 2;
            int y1 = offsetY + radius / 2;
            int y2 = offsetY + spacing + radius + radius / 2;

            Pen p = new Pen(Color.White, 2);
            g.DrawLine(p, x1, y1, x2, y2);
            g.DrawLine(p, x2, y1, x1, y2);

            bool engines = false;// QuadRemote.Core.RemoteController.instance().latestState.enginesOn;

            float e = 0;// QuadRemote.Core.RemoteController.instance().latestState.engine1;
            r1 += e;
            drawEngine(g, x1, y1,radius,e,engines, r1);
            //e = QuadRemote.Core.RemoteController.instance().latestState.engine2;
            r2 -= e;
            drawEngine(g, x2, y1, radius, e, engines, r2);
            //e = QuadRemote.Core.RemoteController.instance().latestState.engine3;
            r3 += e;
            drawEngine(g, x2, y2, radius, e, engines, r3);
            //e = QuadRemote.Core.RemoteController.instance().latestState.engine4;
            r4 -= e;
            drawEngine(g, x1, y2, radius, e, engines, r4);

            g.FillEllipse(Brushes.White, offsetX + radius + spacing / 2 - 16, offsetY + radius + spacing / 2 - 16, 32, 32);
        }

        public override void PaintPanel(Graphics g)
        {
            drawEngines(g, 6, 6,54,20);
            drawLipos(g, 215, 117, 40);

            mode.Text = "-"; // Core.RemoteController.instance().latestState.imuMode.ToString();

            panelColor = Color.Cyan;
            reposition();
            /*if (Core.RemoteController.instance().isConnected() && panelColor != Color.Cyan)
            {
                panelColor = Color.Cyan;
                reposition();
            }
            else if (!Core.RemoteController.instance().isConnected() && panelColor != Color.Red)
            {
                panelColor = Color.Red;
                reposition();
            }*/
        }

    }
}
